﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Easy.BLL;

namespace Easy.Web.Service.Data
{
    /// <summary>
    /// ExportExcel 的摘要说明
    /// </summary>
    public class ExportExcel : BaseHttpHandler
    {

        public override void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string order = context.Request["order"];
            string entityName = context.Request["entityname"];
            string filterData = context.Request["filter"];

            string viewName = context.Request["viewname"];
            if (string.IsNullOrWhiteSpace(viewName))
            {
                viewName = "DefaultView";
            }

            int pageNum = 0;

            if (context.Request["pagenum"] != null)
            {
                pageNum = int.Parse(context.Request["pagenum"].ToString());
            }

            int pageSize = 10;
            if (context.Request["pagesize"] != null)
            {
                pageSize = int.Parse(context.Request["pagesize"].ToString());
            }

            ExportEXCELBLL bll = new ExportEXCELBLL(entityName, this.CurrentUserId);
            context.Response.ContentType = "application/vnd.ms-excel";
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Guid.NewGuid().ToString() + "-" + DateTime.Now.ToString("D") + ".xlsx", Encoding.UTF8));
            context.Response.BinaryWrite(bll.ExportViewpageData(viewName, filterData, order, pageNum, pageSize).ToArray());
            context.Response.End();
        }
    }
}